package Sequence;

import java.util.ArrayDeque;

public class Test0337 {
}

class Solution0337 {
    int pre1 = 0;
    int pre2 = 0;
    public int rob(TreeNode root) {
        dfs(root);
        return pre1;
    }

    private void dfs(TreeNode root) {
        if (root == null){
            pre1=0;
            pre2=0;
            return;
        }

        int now = root.val;
        int tempPre2=0;
        dfs(root.left);
        tempPre2 += pre1;
        now += pre2;
        dfs(root.right);
        tempPre2 += pre1;
        now += pre2;
        now = Math.max(now,tempPre2);
        pre1 = now;
        pre2 = tempPre2;
    }
}